


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SubSonic.DataProviders;
using SubSonic.Extensions;
using System.Linq.Expressions;
using SubSonic.Schema;
using System.Collections;
using SubSonic;
using SubSonic.Repository;
using System.ComponentModel;
using System.Data.Common;

namespace Vit.Web.Models
{
    
    
    /// <summary>
    /// A class which represents the Portal table in the Vit Database.
    /// </summary>
    public partial class Portal: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Portal> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Portal>(new Vit.Web.Models.VitDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Portal> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(Portal item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Portal item=new Portal();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Portal> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Vit.Web.Models.VitDB _db;
        public Portal(string connectionString, string providerName) {

            _db=new Vit.Web.Models.VitDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Portal.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Portal>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Portal(){
             _db=new Vit.Web.Models.VitDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Portal(Expression<Func<Portal, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Portal> GetRepo(string connectionString, string providerName){
            Vit.Web.Models.VitDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Vit.Web.Models.VitDB();
            }else{
                db=new Vit.Web.Models.VitDB(connectionString, providerName);
            }
            IRepository<Portal> _repo;
            
            if(db.TestMode){
                Portal.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Portal>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Portal> GetRepo(){
            return GetRepo("","");
        }
        
        public static Portal SingleOrDefault(Expression<Func<Portal, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Portal single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Portal SingleOrDefault(Expression<Func<Portal, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Portal single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Portal, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Portal, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Portal> Find(Expression<Func<Portal, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Portal> Find(Expression<Func<Portal, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Portal> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Portal> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Portal> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Portal> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Portal> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Portal> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "PortalID";
        }

        public object KeyValue()
        {
            return this.PortalID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<Guid>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Name.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Portal)){
                Portal compare=(Portal)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public string DescriptorValue()
        {
                            return this.Name.ToString();
                    }

        public string DescriptorColumn() {
            return "Name";
        }
        public static string GetKeyColumn()
        {
            return "PortalID";
        }        
        public static string GetDescriptorColumn()
        {
            return "Name";
        }
        
        #region ' Foreign Keys '
        public IQueryable<WebSite> WebSites
        {
            get
            {
                
                  var repo=Vit.Web.Models.WebSite.GetRepo();
                  return from items in repo.GetAll()
                       where items.PortalID == _PortalID
                       select items;
            }
        }

        #endregion
        

        Guid _PortalID;
        public Guid PortalID
        {
            get { return _PortalID; }
            set
            {
                if(_PortalID!=value){
                    _PortalID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PortalID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Name;
        public string Name
        {
            get { return _Name; }
            set
            {
                if(_Name!=value){
                    _Name=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Name");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Description;
        public string Description
        {
            get { return _Description; }
            set
            {
                if(_Description!=value){
                    _Description=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Description");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Logo;
        public string Logo
        {
            get { return _Logo; }
            set
            {
                if(_Logo!=value){
                    _Logo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Logo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Url;
        public string Url
        {
            get { return _Url; }
            set
            {
                if(_Url!=value){
                    _Url=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Url");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid _DefaultTheme;
        public Guid DefaultTheme
        {
            get { return _DefaultTheme; }
            set
            {
                if(_DefaultTheme!=value){
                    _DefaultTheme=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="DefaultTheme");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid _DefaultLocale;
        public Guid DefaultLocale
        {
            get { return _DefaultLocale; }
            set
            {
                if(_DefaultLocale!=value){
                    _DefaultLocale=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="DefaultLocale");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Status;
        public int Status
        {
            get { return _Status; }
            set
            {
                if(_Status!=value){
                    _Status=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Status");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid? _CreatedByUserID;
        public Guid? CreatedByUserID
        {
            get { return _CreatedByUserID; }
            set
            {
                if(_CreatedByUserID!=value){
                    _CreatedByUserID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CreatedByUserID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _CreatedOnDate;
        public DateTime? CreatedOnDate
        {
            get { return _CreatedOnDate; }
            set
            {
                if(_CreatedOnDate!=value){
                    _CreatedOnDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CreatedOnDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid? _LastModifiedByUserID;
        public Guid? LastModifiedByUserID
        {
            get { return _LastModifiedByUserID; }
            set
            {
                if(_LastModifiedByUserID!=value){
                    _LastModifiedByUserID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LastModifiedByUserID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _LastModifiedOnDate;
        public DateTime? LastModifiedOnDate
        {
            get { return _LastModifiedOnDate; }
            set
            {
                if(_LastModifiedOnDate!=value){
                    _LastModifiedOnDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LastModifiedOnDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Portal, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Role table in the Vit Database.
    /// </summary>
    public partial class Role: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Role> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Role>(new Vit.Web.Models.VitDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Role> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(Role item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Role item=new Role();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Role> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Vit.Web.Models.VitDB _db;
        public Role(string connectionString, string providerName) {

            _db=new Vit.Web.Models.VitDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Role.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Role>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Role(){
             _db=new Vit.Web.Models.VitDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Role(Expression<Func<Role, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Role> GetRepo(string connectionString, string providerName){
            Vit.Web.Models.VitDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Vit.Web.Models.VitDB();
            }else{
                db=new Vit.Web.Models.VitDB(connectionString, providerName);
            }
            IRepository<Role> _repo;
            
            if(db.TestMode){
                Role.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Role>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Role> GetRepo(){
            return GetRepo("","");
        }
        
        public static Role SingleOrDefault(Expression<Func<Role, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Role single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Role SingleOrDefault(Expression<Func<Role, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Role single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Role, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Role, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Role> Find(Expression<Func<Role, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Role> Find(Expression<Func<Role, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Role> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Role> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Role> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Role> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Role> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Role> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "RoleID";
        }

        public object KeyValue()
        {
            return this.RoleID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<Guid>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Name.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Role)){
                Role compare=(Role)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public string DescriptorValue()
        {
                            return this.Name.ToString();
                    }

        public string DescriptorColumn() {
            return "Name";
        }
        public static string GetKeyColumn()
        {
            return "RoleID";
        }        
        public static string GetDescriptorColumn()
        {
            return "Name";
        }
        
        #region ' Foreign Keys '
        public IQueryable<RolePermission> RolePermissions
        {
            get
            {
                
                  var repo=Vit.Web.Models.RolePermission.GetRepo();
                  return from items in repo.GetAll()
                       where items.RoleID == _RoleID
                       select items;
            }
        }

        public IQueryable<UserRole> UserRoles
        {
            get
            {
                
                  var repo=Vit.Web.Models.UserRole.GetRepo();
                  return from items in repo.GetAll()
                       where items.RoleID == _RoleID
                       select items;
            }
        }

        public IQueryable<WebSiteRole> WebSiteRoles
        {
            get
            {
                
                  var repo=Vit.Web.Models.WebSiteRole.GetRepo();
                  return from items in repo.GetAll()
                       where items.RoleID == _RoleID
                       select items;
            }
        }

        #endregion
        

        Guid _RoleID;
        public Guid RoleID
        {
            get { return _RoleID; }
            set
            {
                if(_RoleID!=value){
                    _RoleID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="RoleID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Name;
        public string Name
        {
            get { return _Name; }
            set
            {
                if(_Name!=value){
                    _Name=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Name");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Description;
        public string Description
        {
            get { return _Description; }
            set
            {
                if(_Description!=value){
                    _Description=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Description");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Status;
        public int Status
        {
            get { return _Status; }
            set
            {
                if(_Status!=value){
                    _Status=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Status");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Role, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the RolePermission table in the Vit Database.
    /// </summary>
    public partial class RolePermission: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<RolePermission> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<RolePermission>(new Vit.Web.Models.VitDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<RolePermission> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(RolePermission item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                RolePermission item=new RolePermission();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<RolePermission> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Vit.Web.Models.VitDB _db;
        public RolePermission(string connectionString, string providerName) {

            _db=new Vit.Web.Models.VitDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                RolePermission.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<RolePermission>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public RolePermission(){
             _db=new Vit.Web.Models.VitDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public RolePermission(Expression<Func<RolePermission, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<RolePermission> GetRepo(string connectionString, string providerName){
            Vit.Web.Models.VitDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Vit.Web.Models.VitDB();
            }else{
                db=new Vit.Web.Models.VitDB(connectionString, providerName);
            }
            IRepository<RolePermission> _repo;
            
            if(db.TestMode){
                RolePermission.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<RolePermission>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<RolePermission> GetRepo(){
            return GetRepo("","");
        }
        
        public static RolePermission SingleOrDefault(Expression<Func<RolePermission, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            RolePermission single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static RolePermission SingleOrDefault(Expression<Func<RolePermission, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            RolePermission single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<RolePermission, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<RolePermission, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<RolePermission> Find(Expression<Func<RolePermission, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<RolePermission> Find(Expression<Func<RolePermission, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<RolePermission> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<RolePermission> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<RolePermission> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<RolePermission> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<RolePermission> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<RolePermission> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ObjectType";
        }

        public object KeyValue()
        {
            return this.ObjectType;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<string>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.ObjectType.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(RolePermission)){
                RolePermission compare=(RolePermission)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public string DescriptorValue()
        {
                            return this.ObjectType.ToString();
                    }

        public string DescriptorColumn() {
            return "ObjectType";
        }
        public static string GetKeyColumn()
        {
            return "ObjectType";
        }        
        public static string GetDescriptorColumn()
        {
            return "ObjectType";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Role> Roles
        {
            get
            {
                
                  var repo=Vit.Web.Models.Role.GetRepo();
                  return from items in repo.GetAll()
                       where items.RoleID == _RoleID
                       select items;
            }
        }

        #endregion
        

        Guid _RoleID;
        public Guid RoleID
        {
            get { return _RoleID; }
            set
            {
                if(_RoleID!=value){
                    _RoleID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="RoleID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ObjectType;
        public string ObjectType
        {
            get { return _ObjectType; }
            set
            {
                if(_ObjectType!=value){
                    _ObjectType=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ObjectType");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ObjectUri;
        public string ObjectUri
        {
            get { return _ObjectUri; }
            set
            {
                if(_ObjectUri!=value){
                    _ObjectUri=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ObjectUri");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<RolePermission, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Settings table in the Vit Database.
    /// </summary>
    public partial class Setting: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Setting> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Setting>(new Vit.Web.Models.VitDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Setting> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(Setting item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Setting item=new Setting();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Setting> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Vit.Web.Models.VitDB _db;
        public Setting(string connectionString, string providerName) {

            _db=new Vit.Web.Models.VitDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Setting.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Setting>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Setting(){
             _db=new Vit.Web.Models.VitDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Setting(Expression<Func<Setting, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Setting> GetRepo(string connectionString, string providerName){
            Vit.Web.Models.VitDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Vit.Web.Models.VitDB();
            }else{
                db=new Vit.Web.Models.VitDB(connectionString, providerName);
            }
            IRepository<Setting> _repo;
            
            if(db.TestMode){
                Setting.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Setting>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Setting> GetRepo(){
            return GetRepo("","");
        }
        
        public static Setting SingleOrDefault(Expression<Func<Setting, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Setting single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Setting SingleOrDefault(Expression<Func<Setting, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Setting single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Setting, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Setting, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Setting> Find(Expression<Func<Setting, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Setting> Find(Expression<Func<Setting, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Setting> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Setting> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Setting> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Setting> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Setting> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Setting> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "SettingsID";
        }

        public object KeyValue()
        {
            return this.SettingsID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<Guid>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Category.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Setting)){
                Setting compare=(Setting)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public string DescriptorValue()
        {
                            return this.Category.ToString();
                    }

        public string DescriptorColumn() {
            return "Category";
        }
        public static string GetKeyColumn()
        {
            return "SettingsID";
        }        
        public static string GetDescriptorColumn()
        {
            return "Category";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        Guid _SettingsID;
        public Guid SettingsID
        {
            get { return _SettingsID; }
            set
            {
                if(_SettingsID!=value){
                    _SettingsID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SettingsID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Category;
        public string Category
        {
            get { return _Category; }
            set
            {
                if(_Category!=value){
                    _Category=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Category");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Name;
        public string Name
        {
            get { return _Name; }
            set
            {
                if(_Name!=value){
                    _Name=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Name");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Code;
        public string Code
        {
            get { return _Code; }
            set
            {
                if(_Code!=value){
                    _Code=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Code");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Value;
        public string Value
        {
            get { return _Value; }
            set
            {
                if(_Value!=value){
                    _Value=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Value");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _ShowOrder;
        public int ShowOrder
        {
            get { return _ShowOrder; }
            set
            {
                if(_ShowOrder!=value){
                    _ShowOrder=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ShowOrder");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Setting, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the SiteModule table in the Vit Database.
    /// </summary>
    public partial class SiteModule: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<SiteModule> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<SiteModule>(new Vit.Web.Models.VitDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<SiteModule> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(SiteModule item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                SiteModule item=new SiteModule();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<SiteModule> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Vit.Web.Models.VitDB _db;
        public SiteModule(string connectionString, string providerName) {

            _db=new Vit.Web.Models.VitDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                SiteModule.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<SiteModule>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public SiteModule(){
             _db=new Vit.Web.Models.VitDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public SiteModule(Expression<Func<SiteModule, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<SiteModule> GetRepo(string connectionString, string providerName){
            Vit.Web.Models.VitDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Vit.Web.Models.VitDB();
            }else{
                db=new Vit.Web.Models.VitDB(connectionString, providerName);
            }
            IRepository<SiteModule> _repo;
            
            if(db.TestMode){
                SiteModule.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<SiteModule>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<SiteModule> GetRepo(){
            return GetRepo("","");
        }
        
        public static SiteModule SingleOrDefault(Expression<Func<SiteModule, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            SiteModule single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static SiteModule SingleOrDefault(Expression<Func<SiteModule, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            SiteModule single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<SiteModule, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<SiteModule, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<SiteModule> Find(Expression<Func<SiteModule, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<SiteModule> Find(Expression<Func<SiteModule, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<SiteModule> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<SiteModule> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<SiteModule> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<SiteModule> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<SiteModule> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<SiteModule> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ModuleID";
        }

        public object KeyValue()
        {
            return this.ModuleID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<Guid>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.ModuleID.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(SiteModule)){
                SiteModule compare=(SiteModule)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public string DescriptorValue()
        {
                            return this.ModuleID.ToString();
                    }

        public string DescriptorColumn() {
            return "ModuleID";
        }
        public static string GetKeyColumn()
        {
            return "ModuleID";
        }        
        public static string GetDescriptorColumn()
        {
            return "ModuleID";
        }
        
        #region ' Foreign Keys '
        public IQueryable<PageModule> PageModules
        {
            get
            {
                
                  var repo=Vit.Web.Models.PageModule.GetRepo();
                  return from items in repo.GetAll()
                       where items.ModuleID == _ModuleID
                       select items;
            }
        }

        public IQueryable<PageModule> PageModules1
        {
            get
            {
                
                  var repo=Vit.Web.Models.PageModule.GetRepo();
                  return from items in repo.GetAll()
                       where items.SiteID == _ModuleID
                       select items;
            }
        }

        public IQueryable<PageModule> PageModules2
        {
            get
            {
                
                  var repo=Vit.Web.Models.PageModule.GetRepo();
                  return from items in repo.GetAll()
                       where items.ModuleID == _SiteID
                       select items;
            }
        }

        public IQueryable<PageModule> PageModules3
        {
            get
            {
                
                  var repo=Vit.Web.Models.PageModule.GetRepo();
                  return from items in repo.GetAll()
                       where items.SiteID == _SiteID
                       select items;
            }
        }

        public IQueryable<WebSite> WebSites
        {
            get
            {
                
                  var repo=Vit.Web.Models.WebSite.GetRepo();
                  return from items in repo.GetAll()
                       where items.SiteID == _SiteID
                       select items;
            }
        }

        #endregion
        

        Guid _SiteID;
        public Guid SiteID
        {
            get { return _SiteID; }
            set
            {
                if(_SiteID!=value){
                    _SiteID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SiteID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid _ModuleID;
        public Guid ModuleID
        {
            get { return _ModuleID; }
            set
            {
                if(_ModuleID!=value){
                    _ModuleID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ModuleID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _InstalledOnDate;
        public DateTime? InstalledOnDate
        {
            get { return _InstalledOnDate; }
            set
            {
                if(_InstalledOnDate!=value){
                    _InstalledOnDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="InstalledOnDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _ExpiredOnDate;
        public DateTime? ExpiredOnDate
        {
            get { return _ExpiredOnDate; }
            set
            {
                if(_ExpiredOnDate!=value){
                    _ExpiredOnDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ExpiredOnDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<SiteModule, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Skin table in the Vit Database.
    /// </summary>
    public partial class Skin: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Skin> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Skin>(new Vit.Web.Models.VitDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Skin> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(Skin item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Skin item=new Skin();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Skin> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Vit.Web.Models.VitDB _db;
        public Skin(string connectionString, string providerName) {

            _db=new Vit.Web.Models.VitDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Skin.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Skin>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Skin(){
             _db=new Vit.Web.Models.VitDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Skin(Expression<Func<Skin, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Skin> GetRepo(string connectionString, string providerName){
            Vit.Web.Models.VitDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Vit.Web.Models.VitDB();
            }else{
                db=new Vit.Web.Models.VitDB(connectionString, providerName);
            }
            IRepository<Skin> _repo;
            
            if(db.TestMode){
                Skin.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Skin>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Skin> GetRepo(){
            return GetRepo("","");
        }
        
        public static Skin SingleOrDefault(Expression<Func<Skin, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Skin single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Skin SingleOrDefault(Expression<Func<Skin, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Skin single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Skin, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Skin, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Skin> Find(Expression<Func<Skin, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Skin> Find(Expression<Func<Skin, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Skin> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Skin> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Skin> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Skin> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Skin> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Skin> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "SkinID";
        }

        public object KeyValue()
        {
            return this.SkinID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<Guid>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Name.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Skin)){
                Skin compare=(Skin)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public string DescriptorValue()
        {
                            return this.Name.ToString();
                    }

        public string DescriptorColumn() {
            return "Name";
        }
        public static string GetKeyColumn()
        {
            return "SkinID";
        }        
        public static string GetDescriptorColumn()
        {
            return "Name";
        }
        
        #region ' Foreign Keys '
        public IQueryable<User> Users
        {
            get
            {
                
                  var repo=Vit.Web.Models.User.GetRepo();
                  return from items in repo.GetAll()
                       where items.SkinID == _SkinID
                       select items;
            }
        }

        public IQueryable<Theme> Themes
        {
            get
            {
                
                  var repo=Vit.Web.Models.Theme.GetRepo();
                  return from items in repo.GetAll()
                       where items.ThemeID == _ThemeID
                       select items;
            }
        }

        #endregion
        

        Guid _SkinID;
        public Guid SkinID
        {
            get { return _SkinID; }
            set
            {
                if(_SkinID!=value){
                    _SkinID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SkinID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid _ThemeID;
        public Guid ThemeID
        {
            get { return _ThemeID; }
            set
            {
                if(_ThemeID!=value){
                    _ThemeID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ThemeID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Name;
        public string Name
        {
            get { return _Name; }
            set
            {
                if(_Name!=value){
                    _Name=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Name");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Description;
        public string Description
        {
            get { return _Description; }
            set
            {
                if(_Description!=value){
                    _Description=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Description");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Url;
        public string Url
        {
            get { return _Url; }
            set
            {
                if(_Url!=value){
                    _Url=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Url");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Demo;
        public string Demo
        {
            get { return _Demo; }
            set
            {
                if(_Demo!=value){
                    _Demo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Demo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Status;
        public int Status
        {
            get { return _Status; }
            set
            {
                if(_Status!=value){
                    _Status=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Status");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid? _CreatedByUserID;
        public Guid? CreatedByUserID
        {
            get { return _CreatedByUserID; }
            set
            {
                if(_CreatedByUserID!=value){
                    _CreatedByUserID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CreatedByUserID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _CreatedOnDate;
        public DateTime? CreatedOnDate
        {
            get { return _CreatedOnDate; }
            set
            {
                if(_CreatedOnDate!=value){
                    _CreatedOnDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CreatedOnDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid? _LastModifiedByUserID;
        public Guid? LastModifiedByUserID
        {
            get { return _LastModifiedByUserID; }
            set
            {
                if(_LastModifiedByUserID!=value){
                    _LastModifiedByUserID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LastModifiedByUserID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _LastModifiedOnDate;
        public DateTime? LastModifiedOnDate
        {
            get { return _LastModifiedOnDate; }
            set
            {
                if(_LastModifiedOnDate!=value){
                    _LastModifiedOnDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LastModifiedOnDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Skin, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Theme table in the Vit Database.
    /// </summary>
    public partial class Theme: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Theme> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Theme>(new Vit.Web.Models.VitDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Theme> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(Theme item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Theme item=new Theme();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Theme> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Vit.Web.Models.VitDB _db;
        public Theme(string connectionString, string providerName) {

            _db=new Vit.Web.Models.VitDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Theme.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Theme>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Theme(){
             _db=new Vit.Web.Models.VitDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Theme(Expression<Func<Theme, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Theme> GetRepo(string connectionString, string providerName){
            Vit.Web.Models.VitDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Vit.Web.Models.VitDB();
            }else{
                db=new Vit.Web.Models.VitDB(connectionString, providerName);
            }
            IRepository<Theme> _repo;
            
            if(db.TestMode){
                Theme.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Theme>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Theme> GetRepo(){
            return GetRepo("","");
        }
        
        public static Theme SingleOrDefault(Expression<Func<Theme, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Theme single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Theme SingleOrDefault(Expression<Func<Theme, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Theme single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Theme, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Theme, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Theme> Find(Expression<Func<Theme, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Theme> Find(Expression<Func<Theme, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Theme> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Theme> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Theme> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Theme> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Theme> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Theme> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ThemeID";
        }

        public object KeyValue()
        {
            return this.ThemeID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<Guid>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Name.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Theme)){
                Theme compare=(Theme)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public string DescriptorValue()
        {
                            return this.Name.ToString();
                    }

        public string DescriptorColumn() {
            return "Name";
        }
        public static string GetKeyColumn()
        {
            return "ThemeID";
        }        
        public static string GetDescriptorColumn()
        {
            return "Name";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Skin> Skins
        {
            get
            {
                
                  var repo=Vit.Web.Models.Skin.GetRepo();
                  return from items in repo.GetAll()
                       where items.ThemeID == _ThemeID
                       select items;
            }
        }

        public IQueryable<User> Users
        {
            get
            {
                
                  var repo=Vit.Web.Models.User.GetRepo();
                  return from items in repo.GetAll()
                       where items.ThemeID == _ThemeID
                       select items;
            }
        }

        public IQueryable<WebSiteTheme> WebSiteThemes
        {
            get
            {
                
                  var repo=Vit.Web.Models.WebSiteTheme.GetRepo();
                  return from items in repo.GetAll()
                       where items.ThemeID == _ThemeID
                       select items;
            }
        }

        #endregion
        

        Guid _ThemeID;
        public Guid ThemeID
        {
            get { return _ThemeID; }
            set
            {
                if(_ThemeID!=value){
                    _ThemeID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ThemeID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Name;
        public string Name
        {
            get { return _Name; }
            set
            {
                if(_Name!=value){
                    _Name=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Name");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Description;
        public string Description
        {
            get { return _Description; }
            set
            {
                if(_Description!=value){
                    _Description=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Description");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Url;
        public string Url
        {
            get { return _Url; }
            set
            {
                if(_Url!=value){
                    _Url=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Url");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Demo;
        public string Demo
        {
            get { return _Demo; }
            set
            {
                if(_Demo!=value){
                    _Demo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Demo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Layout;
        public string Layout
        {
            get { return _Layout; }
            set
            {
                if(_Layout!=value){
                    _Layout=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Layout");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid _DefaultSkin;
        public Guid DefaultSkin
        {
            get { return _DefaultSkin; }
            set
            {
                if(_DefaultSkin!=value){
                    _DefaultSkin=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="DefaultSkin");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Status;
        public int Status
        {
            get { return _Status; }
            set
            {
                if(_Status!=value){
                    _Status=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Status");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid? _CreatedByUserID;
        public Guid? CreatedByUserID
        {
            get { return _CreatedByUserID; }
            set
            {
                if(_CreatedByUserID!=value){
                    _CreatedByUserID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CreatedByUserID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _CreatedOnDate;
        public DateTime? CreatedOnDate
        {
            get { return _CreatedOnDate; }
            set
            {
                if(_CreatedOnDate!=value){
                    _CreatedOnDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CreatedOnDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid? _LastModifiedByUserID;
        public Guid? LastModifiedByUserID
        {
            get { return _LastModifiedByUserID; }
            set
            {
                if(_LastModifiedByUserID!=value){
                    _LastModifiedByUserID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LastModifiedByUserID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _LastModifiedOnDate;
        public DateTime? LastModifiedOnDate
        {
            get { return _LastModifiedOnDate; }
            set
            {
                if(_LastModifiedOnDate!=value){
                    _LastModifiedOnDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LastModifiedOnDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Theme, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the User table in the Vit Database.
    /// </summary>
    public partial class User: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<User> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<User>(new Vit.Web.Models.VitDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<User> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(User item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                User item=new User();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<User> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Vit.Web.Models.VitDB _db;
        public User(string connectionString, string providerName) {

            _db=new Vit.Web.Models.VitDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                User.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<User>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public User(){
             _db=new Vit.Web.Models.VitDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public User(Expression<Func<User, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<User> GetRepo(string connectionString, string providerName){
            Vit.Web.Models.VitDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Vit.Web.Models.VitDB();
            }else{
                db=new Vit.Web.Models.VitDB(connectionString, providerName);
            }
            IRepository<User> _repo;
            
            if(db.TestMode){
                User.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<User>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<User> GetRepo(){
            return GetRepo("","");
        }
        
        public static User SingleOrDefault(Expression<Func<User, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            User single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static User SingleOrDefault(Expression<Func<User, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            User single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<User, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<User, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<User> Find(Expression<Func<User, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<User> Find(Expression<Func<User, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<User> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<User> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<User> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<User> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<User> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<User> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "UserID";
        }

        public object KeyValue()
        {
            return this.UserID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<Guid>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.FirstName.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(User)){
                User compare=(User)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public string DescriptorValue()
        {
                            return this.FirstName.ToString();
                    }

        public string DescriptorColumn() {
            return "FirstName";
        }
        public static string GetKeyColumn()
        {
            return "UserID";
        }        
        public static string GetDescriptorColumn()
        {
            return "FirstName";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Locale> Locales
        {
            get
            {
                
                  var repo=Vit.Web.Models.Locale.GetRepo();
                  return from items in repo.GetAll()
                       where items.LocaleID == _LocaleID
                       select items;
            }
        }

        public IQueryable<Skin> Skins
        {
            get
            {
                
                  var repo=Vit.Web.Models.Skin.GetRepo();
                  return from items in repo.GetAll()
                       where items.SkinID == _SkinID
                       select items;
            }
        }

        public IQueryable<Theme> Themes
        {
            get
            {
                
                  var repo=Vit.Web.Models.Theme.GetRepo();
                  return from items in repo.GetAll()
                       where items.ThemeID == _ThemeID
                       select items;
            }
        }

        public IQueryable<UserRole> UserRoles
        {
            get
            {
                
                  var repo=Vit.Web.Models.UserRole.GetRepo();
                  return from items in repo.GetAll()
                       where items.UserID == _UserID
                       select items;
            }
        }

        public IQueryable<WebSiteUser> WebSiteUsers
        {
            get
            {
                
                  var repo=Vit.Web.Models.WebSiteUser.GetRepo();
                  return from items in repo.GetAll()
                       where items.UserID == _UserID
                       select items;
            }
        }

        #endregion
        

        Guid _UserID;
        public Guid UserID
        {
            get { return _UserID; }
            set
            {
                if(_UserID!=value){
                    _UserID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UserID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _FirstName;
        public string FirstName
        {
            get { return _FirstName; }
            set
            {
                if(_FirstName!=value){
                    _FirstName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="FirstName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _LastName;
        public string LastName
        {
            get { return _LastName; }
            set
            {
                if(_LastName!=value){
                    _LastName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LastName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _Sex;
        public int? Sex
        {
            get { return _Sex; }
            set
            {
                if(_Sex!=value){
                    _Sex=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Sex");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Email;
        public string Email
        {
            get { return _Email; }
            set
            {
                if(_Email!=value){
                    _Email=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Email");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Account;
        public string Account
        {
            get { return _Account; }
            set
            {
                if(_Account!=value){
                    _Account=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Account");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Password;
        public string Password
        {
            get { return _Password; }
            set
            {
                if(_Password!=value){
                    _Password=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Password");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _PasswordFormat;
        public string PasswordFormat
        {
            get { return _PasswordFormat; }
            set
            {
                if(_PasswordFormat!=value){
                    _PasswordFormat=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PasswordFormat");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _PasswordSalt;
        public string PasswordSalt
        {
            get { return _PasswordSalt; }
            set
            {
                if(_PasswordSalt!=value){
                    _PasswordSalt=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PasswordSalt");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _PasswordQuestion;
        public string PasswordQuestion
        {
            get { return _PasswordQuestion; }
            set
            {
                if(_PasswordQuestion!=value){
                    _PasswordQuestion=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PasswordQuestion");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _PasswordAnswer;
        public string PasswordAnswer
        {
            get { return _PasswordAnswer; }
            set
            {
                if(_PasswordAnswer!=value){
                    _PasswordAnswer=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PasswordAnswer");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _LoggedUrl;
        public string LoggedUrl
        {
            get { return _LoggedUrl; }
            set
            {
                if(_LoggedUrl!=value){
                    _LoggedUrl=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LoggedUrl");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Gravatar;
        public string Gravatar
        {
            get { return _Gravatar; }
            set
            {
                if(_Gravatar!=value){
                    _Gravatar=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Gravatar");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Description;
        public string Description
        {
            get { return _Description; }
            set
            {
                if(_Description!=value){
                    _Description=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Description");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _MSN;
        public string MSN
        {
            get { return _MSN; }
            set
            {
                if(_MSN!=value){
                    _MSN=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MSN");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _QQ;
        public string QQ
        {
            get { return _QQ; }
            set
            {
                if(_QQ!=value){
                    _QQ=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="QQ");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Feixin;
        public string Feixin
        {
            get { return _Feixin; }
            set
            {
                if(_Feixin!=value){
                    _Feixin=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Feixin");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Skype;
        public string Skype
        {
            get { return _Skype; }
            set
            {
                if(_Skype!=value){
                    _Skype=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Skype");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Twitter;
        public string Twitter
        {
            get { return _Twitter; }
            set
            {
                if(_Twitter!=value){
                    _Twitter=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Twitter");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _FriendFeed;
        public string FriendFeed
        {
            get { return _FriendFeed; }
            set
            {
                if(_FriendFeed!=value){
                    _FriendFeed=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="FriendFeed");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Blog;
        public string Blog
        {
            get { return _Blog; }
            set
            {
                if(_Blog!=value){
                    _Blog=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Blog");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _OtherEmail;
        public string OtherEmail
        {
            get { return _OtherEmail; }
            set
            {
                if(_OtherEmail!=value){
                    _OtherEmail=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="OtherEmail");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _OtherContacts;
        public string OtherContacts
        {
            get { return _OtherContacts; }
            set
            {
                if(_OtherContacts!=value){
                    _OtherContacts=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="OtherContacts");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Extension;
        public string Extension
        {
            get { return _Extension; }
            set
            {
                if(_Extension!=value){
                    _Extension=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Extension");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _LastLoggedDate;
        public DateTime? LastLoggedDate
        {
            get { return _LastLoggedDate; }
            set
            {
                if(_LastLoggedDate!=value){
                    _LastLoggedDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LastLoggedDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _LastVisitedDate;
        public DateTime? LastVisitedDate
        {
            get { return _LastVisitedDate; }
            set
            {
                if(_LastVisitedDate!=value){
                    _LastVisitedDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LastVisitedDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _LastVisitedUrl;
        public string LastVisitedUrl
        {
            get { return _LastVisitedUrl; }
            set
            {
                if(_LastVisitedUrl!=value){
                    _LastVisitedUrl=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LastVisitedUrl");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Status;
        public int Status
        {
            get { return _Status; }
            set
            {
                if(_Status!=value){
                    _Status=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Status");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _LastStatusDate;
        public DateTime? LastStatusDate
        {
            get { return _LastStatusDate; }
            set
            {
                if(_LastStatusDate!=value){
                    _LastStatusDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LastStatusDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid _LocaleID;
        public Guid LocaleID
        {
            get { return _LocaleID; }
            set
            {
                if(_LocaleID!=value){
                    _LocaleID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LocaleID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid _ThemeID;
        public Guid ThemeID
        {
            get { return _ThemeID; }
            set
            {
                if(_ThemeID!=value){
                    _ThemeID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ThemeID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid _SkinID;
        public Guid SkinID
        {
            get { return _SkinID; }
            set
            {
                if(_SkinID!=value){
                    _SkinID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SkinID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<User, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the UserRole table in the Vit Database.
    /// </summary>
    public partial class UserRole: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<UserRole> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<UserRole>(new Vit.Web.Models.VitDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<UserRole> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(UserRole item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                UserRole item=new UserRole();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<UserRole> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Vit.Web.Models.VitDB _db;
        public UserRole(string connectionString, string providerName) {

            _db=new Vit.Web.Models.VitDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                UserRole.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<UserRole>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public UserRole(){
             _db=new Vit.Web.Models.VitDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public UserRole(Expression<Func<UserRole, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<UserRole> GetRepo(string connectionString, string providerName){
            Vit.Web.Models.VitDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Vit.Web.Models.VitDB();
            }else{
                db=new Vit.Web.Models.VitDB(connectionString, providerName);
            }
            IRepository<UserRole> _repo;
            
            if(db.TestMode){
                UserRole.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<UserRole>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<UserRole> GetRepo(){
            return GetRepo("","");
        }
        
        public static UserRole SingleOrDefault(Expression<Func<UserRole, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            UserRole single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static UserRole SingleOrDefault(Expression<Func<UserRole, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            UserRole single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<UserRole, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<UserRole, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<UserRole> Find(Expression<Func<UserRole, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<UserRole> Find(Expression<Func<UserRole, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<UserRole> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<UserRole> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<UserRole> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<UserRole> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<UserRole> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<UserRole> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "RoleID";
        }

        public object KeyValue()
        {
            return this.RoleID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<Guid>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.RoleID.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(UserRole)){
                UserRole compare=(UserRole)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public string DescriptorValue()
        {
                            return this.RoleID.ToString();
                    }

        public string DescriptorColumn() {
            return "RoleID";
        }
        public static string GetKeyColumn()
        {
            return "RoleID";
        }        
        public static string GetDescriptorColumn()
        {
            return "RoleID";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Role> Roles
        {
            get
            {
                
                  var repo=Vit.Web.Models.Role.GetRepo();
                  return from items in repo.GetAll()
                       where items.RoleID == _RoleID
                       select items;
            }
        }

        public IQueryable<User> Users
        {
            get
            {
                
                  var repo=Vit.Web.Models.User.GetRepo();
                  return from items in repo.GetAll()
                       where items.UserID == _UserID
                       select items;
            }
        }

        #endregion
        

        Guid _UserID;
        public Guid UserID
        {
            get { return _UserID; }
            set
            {
                if(_UserID!=value){
                    _UserID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UserID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid _RoleID;
        public Guid RoleID
        {
            get { return _RoleID; }
            set
            {
                if(_RoleID!=value){
                    _RoleID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="RoleID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _Status;
        public int? Status
        {
            get { return _Status; }
            set
            {
                if(_Status!=value){
                    _Status=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Status");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _ValidOnDate;
        public DateTime? ValidOnDate
        {
            get { return _ValidOnDate; }
            set
            {
                if(_ValidOnDate!=value){
                    _ValidOnDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ValidOnDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _ExpiredOnDate;
        public DateTime? ExpiredOnDate
        {
            get { return _ExpiredOnDate; }
            set
            {
                if(_ExpiredOnDate!=value){
                    _ExpiredOnDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ExpiredOnDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<UserRole, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the WebSite table in the Vit Database.
    /// </summary>
    public partial class WebSite: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<WebSite> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<WebSite>(new Vit.Web.Models.VitDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<WebSite> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(WebSite item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                WebSite item=new WebSite();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<WebSite> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Vit.Web.Models.VitDB _db;
        public WebSite(string connectionString, string providerName) {

            _db=new Vit.Web.Models.VitDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                WebSite.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<WebSite>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public WebSite(){
             _db=new Vit.Web.Models.VitDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public WebSite(Expression<Func<WebSite, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<WebSite> GetRepo(string connectionString, string providerName){
            Vit.Web.Models.VitDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Vit.Web.Models.VitDB();
            }else{
                db=new Vit.Web.Models.VitDB(connectionString, providerName);
            }
            IRepository<WebSite> _repo;
            
            if(db.TestMode){
                WebSite.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<WebSite>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<WebSite> GetRepo(){
            return GetRepo("","");
        }
        
        public static WebSite SingleOrDefault(Expression<Func<WebSite, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            WebSite single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static WebSite SingleOrDefault(Expression<Func<WebSite, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            WebSite single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<WebSite, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<WebSite, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<WebSite> Find(Expression<Func<WebSite, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<WebSite> Find(Expression<Func<WebSite, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<WebSite> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<WebSite> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<WebSite> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<WebSite> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<WebSite> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<WebSite> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "SiteID";
        }

        public object KeyValue()
        {
            return this.SiteID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<Guid>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Name.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(WebSite)){
                WebSite compare=(WebSite)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public string DescriptorValue()
        {
                            return this.Name.ToString();
                    }

        public string DescriptorColumn() {
            return "Name";
        }
        public static string GetKeyColumn()
        {
            return "SiteID";
        }        
        public static string GetDescriptorColumn()
        {
            return "Name";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Portal> Portals
        {
            get
            {
                
                  var repo=Vit.Web.Models.Portal.GetRepo();
                  return from items in repo.GetAll()
                       where items.PortalID == _PortalID
                       select items;
            }
        }

        public IQueryable<SiteModule> SiteModules
        {
            get
            {
                
                  var repo=Vit.Web.Models.SiteModule.GetRepo();
                  return from items in repo.GetAll()
                       where items.SiteID == _SiteID
                       select items;
            }
        }

        public IQueryable<WebPage> WebPages
        {
            get
            {
                
                  var repo=Vit.Web.Models.WebPage.GetRepo();
                  return from items in repo.GetAll()
                       where items.SiteID == _SiteID
                       select items;
            }
        }

        public IQueryable<WebSiteRole> WebSiteRoles
        {
            get
            {
                
                  var repo=Vit.Web.Models.WebSiteRole.GetRepo();
                  return from items in repo.GetAll()
                       where items.SiteID == _SiteID
                       select items;
            }
        }

        public IQueryable<WebSiteTheme> WebSiteThemes
        {
            get
            {
                
                  var repo=Vit.Web.Models.WebSiteTheme.GetRepo();
                  return from items in repo.GetAll()
                       where items.SiteID == _SiteID
                       select items;
            }
        }

        public IQueryable<WebSiteUser> WebSiteUsers
        {
            get
            {
                
                  var repo=Vit.Web.Models.WebSiteUser.GetRepo();
                  return from items in repo.GetAll()
                       where items.SiteID == _SiteID
                       select items;
            }
        }

        #endregion
        

        Guid _SiteID;
        public Guid SiteID
        {
            get { return _SiteID; }
            set
            {
                if(_SiteID!=value){
                    _SiteID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SiteID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid? _PortalID;
        public Guid? PortalID
        {
            get { return _PortalID; }
            set
            {
                if(_PortalID!=value){
                    _PortalID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PortalID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Name;
        public string Name
        {
            get { return _Name; }
            set
            {
                if(_Name!=value){
                    _Name=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Name");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Description;
        public string Description
        {
            get { return _Description; }
            set
            {
                if(_Description!=value){
                    _Description=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Description");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Logo;
        public string Logo
        {
            get { return _Logo; }
            set
            {
                if(_Logo!=value){
                    _Logo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Logo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _FavIcon;
        public string FavIcon
        {
            get { return _FavIcon; }
            set
            {
                if(_FavIcon!=value){
                    _FavIcon=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="FavIcon");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Url;
        public string Url
        {
            get { return _Url; }
            set
            {
                if(_Url!=value){
                    _Url=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Url");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid _DefaultTheme;
        public Guid DefaultTheme
        {
            get { return _DefaultTheme; }
            set
            {
                if(_DefaultTheme!=value){
                    _DefaultTheme=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="DefaultTheme");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid _DefaultLocale;
        public Guid DefaultLocale
        {
            get { return _DefaultLocale; }
            set
            {
                if(_DefaultLocale!=value){
                    _DefaultLocale=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="DefaultLocale");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Status;
        public int Status
        {
            get { return _Status; }
            set
            {
                if(_Status!=value){
                    _Status=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Status");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid? _CreatedByUserID;
        public Guid? CreatedByUserID
        {
            get { return _CreatedByUserID; }
            set
            {
                if(_CreatedByUserID!=value){
                    _CreatedByUserID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CreatedByUserID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _CreatedOnDate;
        public DateTime? CreatedOnDate
        {
            get { return _CreatedOnDate; }
            set
            {
                if(_CreatedOnDate!=value){
                    _CreatedOnDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CreatedOnDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid? _LastModifiedByUserID;
        public Guid? LastModifiedByUserID
        {
            get { return _LastModifiedByUserID; }
            set
            {
                if(_LastModifiedByUserID!=value){
                    _LastModifiedByUserID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LastModifiedByUserID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _LastModifiedOnDate;
        public DateTime? LastModifiedOnDate
        {
            get { return _LastModifiedOnDate; }
            set
            {
                if(_LastModifiedOnDate!=value){
                    _LastModifiedOnDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LastModifiedOnDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<WebSite, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the WebSiteRole table in the Vit Database.
    /// </summary>
    public partial class WebSiteRole: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<WebSiteRole> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<WebSiteRole>(new Vit.Web.Models.VitDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<WebSiteRole> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(WebSiteRole item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                WebSiteRole item=new WebSiteRole();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<WebSiteRole> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Vit.Web.Models.VitDB _db;
        public WebSiteRole(string connectionString, string providerName) {

            _db=new Vit.Web.Models.VitDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                WebSiteRole.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<WebSiteRole>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public WebSiteRole(){
             _db=new Vit.Web.Models.VitDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public WebSiteRole(Expression<Func<WebSiteRole, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<WebSiteRole> GetRepo(string connectionString, string providerName){
            Vit.Web.Models.VitDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Vit.Web.Models.VitDB();
            }else{
                db=new Vit.Web.Models.VitDB(connectionString, providerName);
            }
            IRepository<WebSiteRole> _repo;
            
            if(db.TestMode){
                WebSiteRole.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<WebSiteRole>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<WebSiteRole> GetRepo(){
            return GetRepo("","");
        }
        
        public static WebSiteRole SingleOrDefault(Expression<Func<WebSiteRole, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            WebSiteRole single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static WebSiteRole SingleOrDefault(Expression<Func<WebSiteRole, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            WebSiteRole single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<WebSiteRole, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<WebSiteRole, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<WebSiteRole> Find(Expression<Func<WebSiteRole, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<WebSiteRole> Find(Expression<Func<WebSiteRole, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<WebSiteRole> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<WebSiteRole> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<WebSiteRole> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<WebSiteRole> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<WebSiteRole> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<WebSiteRole> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "RoleID";
        }

        public object KeyValue()
        {
            return this.RoleID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<Guid>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.RoleID.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(WebSiteRole)){
                WebSiteRole compare=(WebSiteRole)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public string DescriptorValue()
        {
                            return this.RoleID.ToString();
                    }

        public string DescriptorColumn() {
            return "RoleID";
        }
        public static string GetKeyColumn()
        {
            return "RoleID";
        }        
        public static string GetDescriptorColumn()
        {
            return "RoleID";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Role> Roles
        {
            get
            {
                
                  var repo=Vit.Web.Models.Role.GetRepo();
                  return from items in repo.GetAll()
                       where items.RoleID == _RoleID
                       select items;
            }
        }

        public IQueryable<WebSite> WebSites
        {
            get
            {
                
                  var repo=Vit.Web.Models.WebSite.GetRepo();
                  return from items in repo.GetAll()
                       where items.SiteID == _SiteID
                       select items;
            }
        }

        #endregion
        

        Guid _SiteID;
        public Guid SiteID
        {
            get { return _SiteID; }
            set
            {
                if(_SiteID!=value){
                    _SiteID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SiteID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid _RoleID;
        public Guid RoleID
        {
            get { return _RoleID; }
            set
            {
                if(_RoleID!=value){
                    _RoleID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="RoleID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Status;
        public int Status
        {
            get { return _Status; }
            set
            {
                if(_Status!=value){
                    _Status=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Status");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _ValidOnDate;
        public DateTime? ValidOnDate
        {
            get { return _ValidOnDate; }
            set
            {
                if(_ValidOnDate!=value){
                    _ValidOnDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ValidOnDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _ExpiredOnDate;
        public DateTime? ExpiredOnDate
        {
            get { return _ExpiredOnDate; }
            set
            {
                if(_ExpiredOnDate!=value){
                    _ExpiredOnDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ExpiredOnDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<WebSiteRole, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the WebSiteTheme table in the Vit Database.
    /// </summary>
    public partial class WebSiteTheme: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<WebSiteTheme> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<WebSiteTheme>(new Vit.Web.Models.VitDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<WebSiteTheme> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(WebSiteTheme item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                WebSiteTheme item=new WebSiteTheme();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<WebSiteTheme> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Vit.Web.Models.VitDB _db;
        public WebSiteTheme(string connectionString, string providerName) {

            _db=new Vit.Web.Models.VitDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                WebSiteTheme.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<WebSiteTheme>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public WebSiteTheme(){
             _db=new Vit.Web.Models.VitDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public WebSiteTheme(Expression<Func<WebSiteTheme, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<WebSiteTheme> GetRepo(string connectionString, string providerName){
            Vit.Web.Models.VitDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Vit.Web.Models.VitDB();
            }else{
                db=new Vit.Web.Models.VitDB(connectionString, providerName);
            }
            IRepository<WebSiteTheme> _repo;
            
            if(db.TestMode){
                WebSiteTheme.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<WebSiteTheme>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<WebSiteTheme> GetRepo(){
            return GetRepo("","");
        }
        
        public static WebSiteTheme SingleOrDefault(Expression<Func<WebSiteTheme, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            WebSiteTheme single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static WebSiteTheme SingleOrDefault(Expression<Func<WebSiteTheme, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            WebSiteTheme single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<WebSiteTheme, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<WebSiteTheme, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<WebSiteTheme> Find(Expression<Func<WebSiteTheme, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<WebSiteTheme> Find(Expression<Func<WebSiteTheme, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<WebSiteTheme> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<WebSiteTheme> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<WebSiteTheme> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<WebSiteTheme> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<WebSiteTheme> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<WebSiteTheme> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "SiteID";
        }

        public object KeyValue()
        {
            return this.SiteID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<Guid>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.ThemeID.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(WebSiteTheme)){
                WebSiteTheme compare=(WebSiteTheme)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public string DescriptorValue()
        {
                            return this.ThemeID.ToString();
                    }

        public string DescriptorColumn() {
            return "ThemeID";
        }
        public static string GetKeyColumn()
        {
            return "SiteID";
        }        
        public static string GetDescriptorColumn()
        {
            return "ThemeID";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Theme> Themes
        {
            get
            {
                
                  var repo=Vit.Web.Models.Theme.GetRepo();
                  return from items in repo.GetAll()
                       where items.ThemeID == _ThemeID
                       select items;
            }
        }

        public IQueryable<WebSite> WebSites
        {
            get
            {
                
                  var repo=Vit.Web.Models.WebSite.GetRepo();
                  return from items in repo.GetAll()
                       where items.SiteID == _SiteID
                       select items;
            }
        }

        #endregion
        

        Guid _SiteID;
        public Guid SiteID
        {
            get { return _SiteID; }
            set
            {
                if(_SiteID!=value){
                    _SiteID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SiteID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid _ThemeID;
        public Guid ThemeID
        {
            get { return _ThemeID; }
            set
            {
                if(_ThemeID!=value){
                    _ThemeID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ThemeID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Status;
        public int Status
        {
            get { return _Status; }
            set
            {
                if(_Status!=value){
                    _Status=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Status");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<WebSiteTheme, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the WebSiteUser table in the Vit Database.
    /// </summary>
    public partial class WebSiteUser: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<WebSiteUser> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<WebSiteUser>(new Vit.Web.Models.VitDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<WebSiteUser> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(WebSiteUser item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                WebSiteUser item=new WebSiteUser();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<WebSiteUser> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Vit.Web.Models.VitDB _db;
        public WebSiteUser(string connectionString, string providerName) {

            _db=new Vit.Web.Models.VitDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                WebSiteUser.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<WebSiteUser>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public WebSiteUser(){
             _db=new Vit.Web.Models.VitDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public WebSiteUser(Expression<Func<WebSiteUser, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<WebSiteUser> GetRepo(string connectionString, string providerName){
            Vit.Web.Models.VitDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Vit.Web.Models.VitDB();
            }else{
                db=new Vit.Web.Models.VitDB(connectionString, providerName);
            }
            IRepository<WebSiteUser> _repo;
            
            if(db.TestMode){
                WebSiteUser.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<WebSiteUser>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<WebSiteUser> GetRepo(){
            return GetRepo("","");
        }
        
        public static WebSiteUser SingleOrDefault(Expression<Func<WebSiteUser, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            WebSiteUser single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static WebSiteUser SingleOrDefault(Expression<Func<WebSiteUser, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            WebSiteUser single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<WebSiteUser, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<WebSiteUser, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<WebSiteUser> Find(Expression<Func<WebSiteUser, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<WebSiteUser> Find(Expression<Func<WebSiteUser, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<WebSiteUser> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<WebSiteUser> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<WebSiteUser> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<WebSiteUser> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<WebSiteUser> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<WebSiteUser> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "SiteID";
        }

        public object KeyValue()
        {
            return this.SiteID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<Guid>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.UserID.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(WebSiteUser)){
                WebSiteUser compare=(WebSiteUser)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public string DescriptorValue()
        {
                            return this.UserID.ToString();
                    }

        public string DescriptorColumn() {
            return "UserID";
        }
        public static string GetKeyColumn()
        {
            return "SiteID";
        }        
        public static string GetDescriptorColumn()
        {
            return "UserID";
        }
        
        #region ' Foreign Keys '
        public IQueryable<User> Users
        {
            get
            {
                
                  var repo=Vit.Web.Models.User.GetRepo();
                  return from items in repo.GetAll()
                       where items.UserID == _UserID
                       select items;
            }
        }

        public IQueryable<WebSite> WebSites
        {
            get
            {
                
                  var repo=Vit.Web.Models.WebSite.GetRepo();
                  return from items in repo.GetAll()
                       where items.SiteID == _SiteID
                       select items;
            }
        }

        #endregion
        

        Guid _SiteID;
        public Guid SiteID
        {
            get { return _SiteID; }
            set
            {
                if(_SiteID!=value){
                    _SiteID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SiteID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid _UserID;
        public Guid UserID
        {
            get { return _UserID; }
            set
            {
                if(_UserID!=value){
                    _UserID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UserID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Status;
        public int Status
        {
            get { return _Status; }
            set
            {
                if(_Status!=value){
                    _Status=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Status");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _ValidOnDate;
        public DateTime? ValidOnDate
        {
            get { return _ValidOnDate; }
            set
            {
                if(_ValidOnDate!=value){
                    _ValidOnDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ValidOnDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _ExpiredOnDate;
        public DateTime? ExpiredOnDate
        {
            get { return _ExpiredOnDate; }
            set
            {
                if(_ExpiredOnDate!=value){
                    _ExpiredOnDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ExpiredOnDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<WebSiteUser, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the WebPage table in the Vit Database.
    /// </summary>
    public partial class WebPage: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<WebPage> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<WebPage>(new Vit.Web.Models.VitDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<WebPage> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(WebPage item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                WebPage item=new WebPage();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<WebPage> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Vit.Web.Models.VitDB _db;
        public WebPage(string connectionString, string providerName) {

            _db=new Vit.Web.Models.VitDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                WebPage.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<WebPage>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public WebPage(){
             _db=new Vit.Web.Models.VitDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public WebPage(Expression<Func<WebPage, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<WebPage> GetRepo(string connectionString, string providerName){
            Vit.Web.Models.VitDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Vit.Web.Models.VitDB();
            }else{
                db=new Vit.Web.Models.VitDB(connectionString, providerName);
            }
            IRepository<WebPage> _repo;
            
            if(db.TestMode){
                WebPage.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<WebPage>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<WebPage> GetRepo(){
            return GetRepo("","");
        }
        
        public static WebPage SingleOrDefault(Expression<Func<WebPage, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            WebPage single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static WebPage SingleOrDefault(Expression<Func<WebPage, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            WebPage single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<WebPage, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<WebPage, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<WebPage> Find(Expression<Func<WebPage, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<WebPage> Find(Expression<Func<WebPage, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<WebPage> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<WebPage> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<WebPage> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<WebPage> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<WebPage> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<WebPage> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "PageID";
        }

        public object KeyValue()
        {
            return this.PageID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<Guid>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Title.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(WebPage)){
                WebPage compare=(WebPage)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public string DescriptorValue()
        {
                            return this.Title.ToString();
                    }

        public string DescriptorColumn() {
            return "Title";
        }
        public static string GetKeyColumn()
        {
            return "PageID";
        }        
        public static string GetDescriptorColumn()
        {
            return "Title";
        }
        
        #region ' Foreign Keys '
        public IQueryable<PageModule> PageModules
        {
            get
            {
                
                  var repo=Vit.Web.Models.PageModule.GetRepo();
                  return from items in repo.GetAll()
                       where items.PageID == _PageID
                       select items;
            }
        }

        public IQueryable<WebSite> WebSites
        {
            get
            {
                
                  var repo=Vit.Web.Models.WebSite.GetRepo();
                  return from items in repo.GetAll()
                       where items.SiteID == _SiteID
                       select items;
            }
        }

        #endregion
        

        Guid _PageID;
        public Guid PageID
        {
            get { return _PageID; }
            set
            {
                if(_PageID!=value){
                    _PageID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PageID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid _SiteID;
        public Guid SiteID
        {
            get { return _SiteID; }
            set
            {
                if(_SiteID!=value){
                    _SiteID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SiteID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Title;
        public string Title
        {
            get { return _Title; }
            set
            {
                if(_Title!=value){
                    _Title=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Title");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _UrlPattern;
        public string UrlPattern
        {
            get { return _UrlPattern; }
            set
            {
                if(_UrlPattern!=value){
                    _UrlPattern=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UrlPattern");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _MetaKeywords;
        public string MetaKeywords
        {
            get { return _MetaKeywords; }
            set
            {
                if(_MetaKeywords!=value){
                    _MetaKeywords=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MetaKeywords");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _MetaDescription;
        public string MetaDescription
        {
            get { return _MetaDescription; }
            set
            {
                if(_MetaDescription!=value){
                    _MetaDescription=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MetaDescription");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _MetaCharset;
        public string MetaCharset
        {
            get { return _MetaCharset; }
            set
            {
                if(_MetaCharset!=value){
                    _MetaCharset=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MetaCharset");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _MetaCopyRight;
        public string MetaCopyRight
        {
            get { return _MetaCopyRight; }
            set
            {
                if(_MetaCopyRight!=value){
                    _MetaCopyRight=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MetaCopyRight");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _MetaRobots;
        public string MetaRobots
        {
            get { return _MetaRobots; }
            set
            {
                if(_MetaRobots!=value){
                    _MetaRobots=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MetaRobots");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Priority;
        public int Priority
        {
            get { return _Priority; }
            set
            {
                if(_Priority!=value){
                    _Priority=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Priority");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Area;
        public string Area
        {
            get { return _Area; }
            set
            {
                if(_Area!=value){
                    _Area=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Area");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _MasterName;
        public string MasterName
        {
            get { return _MasterName; }
            set
            {
                if(_MasterName!=value){
                    _MasterName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MasterName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ViewName;
        public string ViewName
        {
            get { return _ViewName; }
            set
            {
                if(_ViewName!=value){
                    _ViewName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ViewName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<WebPage, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the PageModule table in the Vit Database.
    /// </summary>
    public partial class PageModule: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<PageModule> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<PageModule>(new Vit.Web.Models.VitDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<PageModule> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(PageModule item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                PageModule item=new PageModule();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<PageModule> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Vit.Web.Models.VitDB _db;
        public PageModule(string connectionString, string providerName) {

            _db=new Vit.Web.Models.VitDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                PageModule.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<PageModule>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public PageModule(){
             _db=new Vit.Web.Models.VitDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public PageModule(Expression<Func<PageModule, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<PageModule> GetRepo(string connectionString, string providerName){
            Vit.Web.Models.VitDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Vit.Web.Models.VitDB();
            }else{
                db=new Vit.Web.Models.VitDB(connectionString, providerName);
            }
            IRepository<PageModule> _repo;
            
            if(db.TestMode){
                PageModule.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<PageModule>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<PageModule> GetRepo(){
            return GetRepo("","");
        }
        
        public static PageModule SingleOrDefault(Expression<Func<PageModule, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            PageModule single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static PageModule SingleOrDefault(Expression<Func<PageModule, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            PageModule single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<PageModule, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<PageModule, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<PageModule> Find(Expression<Func<PageModule, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<PageModule> Find(Expression<Func<PageModule, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<PageModule> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<PageModule> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<PageModule> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<PageModule> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<PageModule> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<PageModule> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ModuleID";
        }

        public object KeyValue()
        {
            return this.ModuleID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<Guid>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Layout.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(PageModule)){
                PageModule compare=(PageModule)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public string DescriptorValue()
        {
                            return this.Layout.ToString();
                    }

        public string DescriptorColumn() {
            return "Layout";
        }
        public static string GetKeyColumn()
        {
            return "ModuleID";
        }        
        public static string GetDescriptorColumn()
        {
            return "Layout";
        }
        
        #region ' Foreign Keys '
        public IQueryable<SiteModule> SiteModules
        {
            get
            {
                
                  var repo=Vit.Web.Models.SiteModule.GetRepo();
                  return from items in repo.GetAll()
                       where items.ModuleID == _ModuleID
                       select items;
            }
        }

        public IQueryable<SiteModule> SiteModules1
        {
            get
            {
                
                  var repo=Vit.Web.Models.SiteModule.GetRepo();
                  return from items in repo.GetAll()
                       where items.ModuleID == _SiteID
                       select items;
            }
        }

        public IQueryable<SiteModule> SiteModules2
        {
            get
            {
                
                  var repo=Vit.Web.Models.SiteModule.GetRepo();
                  return from items in repo.GetAll()
                       where items.SiteID == _ModuleID
                       select items;
            }
        }

        public IQueryable<SiteModule> SiteModules3
        {
            get
            {
                
                  var repo=Vit.Web.Models.SiteModule.GetRepo();
                  return from items in repo.GetAll()
                       where items.SiteID == _SiteID
                       select items;
            }
        }

        public IQueryable<WebPage> WebPages
        {
            get
            {
                
                  var repo=Vit.Web.Models.WebPage.GetRepo();
                  return from items in repo.GetAll()
                       where items.PageID == _PageID
                       select items;
            }
        }

        #endregion
        

        Guid _PageID;
        public Guid PageID
        {
            get { return _PageID; }
            set
            {
                if(_PageID!=value){
                    _PageID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PageID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid _ModuleID;
        public Guid ModuleID
        {
            get { return _ModuleID; }
            set
            {
                if(_ModuleID!=value){
                    _ModuleID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ModuleID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid _SiteID;
        public Guid SiteID
        {
            get { return _SiteID; }
            set
            {
                if(_SiteID!=value){
                    _SiteID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SiteID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Layout;
        public string Layout
        {
            get { return _Layout; }
            set
            {
                if(_Layout!=value){
                    _Layout=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Layout");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Container;
        public string Container
        {
            get { return _Container; }
            set
            {
                if(_Container!=value){
                    _Container=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Container");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Parameter;
        public string Parameter
        {
            get { return _Parameter; }
            set
            {
                if(_Parameter!=value){
                    _Parameter=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Parameter");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _ShowOrder;
        public int ShowOrder
        {
            get { return _ShowOrder; }
            set
            {
                if(_ShowOrder!=value){
                    _ShowOrder=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ShowOrder");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<PageModule, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the BasicData table in the Vit Database.
    /// </summary>
    public partial class BasicDatum: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<BasicDatum> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<BasicDatum>(new Vit.Web.Models.VitDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<BasicDatum> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(BasicDatum item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                BasicDatum item=new BasicDatum();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<BasicDatum> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Vit.Web.Models.VitDB _db;
        public BasicDatum(string connectionString, string providerName) {

            _db=new Vit.Web.Models.VitDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                BasicDatum.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<BasicDatum>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public BasicDatum(){
             _db=new Vit.Web.Models.VitDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public BasicDatum(Expression<Func<BasicDatum, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<BasicDatum> GetRepo(string connectionString, string providerName){
            Vit.Web.Models.VitDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Vit.Web.Models.VitDB();
            }else{
                db=new Vit.Web.Models.VitDB(connectionString, providerName);
            }
            IRepository<BasicDatum> _repo;
            
            if(db.TestMode){
                BasicDatum.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<BasicDatum>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<BasicDatum> GetRepo(){
            return GetRepo("","");
        }
        
        public static BasicDatum SingleOrDefault(Expression<Func<BasicDatum, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            BasicDatum single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static BasicDatum SingleOrDefault(Expression<Func<BasicDatum, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            BasicDatum single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<BasicDatum, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<BasicDatum, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<BasicDatum> Find(Expression<Func<BasicDatum, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<BasicDatum> Find(Expression<Func<BasicDatum, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<BasicDatum> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<BasicDatum> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<BasicDatum> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<BasicDatum> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<BasicDatum> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<BasicDatum> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "BasicDataID";
        }

        public object KeyValue()
        {
            return this.BasicDataID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<Guid>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Category.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(BasicDatum)){
                BasicDatum compare=(BasicDatum)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public string DescriptorValue()
        {
                            return this.Category.ToString();
                    }

        public string DescriptorColumn() {
            return "Category";
        }
        public static string GetKeyColumn()
        {
            return "BasicDataID";
        }        
        public static string GetDescriptorColumn()
        {
            return "Category";
        }
        
        #region ' Foreign Keys '
        public IQueryable<BasicDatum> BasicData
        {
            get
            {
                
                  var repo=Vit.Web.Models.BasicDatum.GetRepo();
                  return from items in repo.GetAll()
                       where items.BasicDataID == _ParentBasicDataID
                       select items;
            }
        }

        #endregion
        

        Guid _BasicDataID;
        public Guid BasicDataID
        {
            get { return _BasicDataID; }
            set
            {
                if(_BasicDataID!=value){
                    _BasicDataID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="BasicDataID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Category;
        public string Category
        {
            get { return _Category; }
            set
            {
                if(_Category!=value){
                    _Category=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Category");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Name;
        public string Name
        {
            get { return _Name; }
            set
            {
                if(_Name!=value){
                    _Name=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Name");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Code;
        public string Code
        {
            get { return _Code; }
            set
            {
                if(_Code!=value){
                    _Code=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Code");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Value;
        public string Value
        {
            get { return _Value; }
            set
            {
                if(_Value!=value){
                    _Value=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Value");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _ShowOrder;
        public int ShowOrder
        {
            get { return _ShowOrder; }
            set
            {
                if(_ShowOrder!=value){
                    _ShowOrder=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ShowOrder");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        Guid? _ParentBasicDataID;
        public Guid? ParentBasicDataID
        {
            get { return _ParentBasicDataID; }
            set
            {
                if(_ParentBasicDataID!=value){
                    _ParentBasicDataID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ParentBasicDataID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<BasicDatum, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the LocalResouce table in the Vit Database.
    /// </summary>
    public partial class LocalResouce: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<LocalResouce> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<LocalResouce>(new Vit.Web.Models.VitDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<LocalResouce> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(LocalResouce item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                LocalResouce item=new LocalResouce();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<LocalResouce> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Vit.Web.Models.VitDB _db;
        public LocalResouce(string connectionString, string providerName) {

            _db=new Vit.Web.Models.VitDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                LocalResouce.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<LocalResouce>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public LocalResouce(){
             _db=new Vit.Web.Models.VitDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public LocalResouce(Expression<Func<LocalResouce, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<LocalResouce> GetRepo(string connectionString, string providerName){
            Vit.Web.Models.VitDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Vit.Web.Models.VitDB();
            }else{
                db=new Vit.Web.Models.VitDB(connectionString, providerName);
            }
            IRepository<LocalResouce> _repo;
            
            if(db.TestMode){
                LocalResouce.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<LocalResouce>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<LocalResouce> GetRepo(){
            return GetRepo("","");
        }
        
        public static LocalResouce SingleOrDefault(Expression<Func<LocalResouce, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            LocalResouce single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static LocalResouce SingleOrDefault(Expression<Func<LocalResouce, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            LocalResouce single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<LocalResouce, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<LocalResouce, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<LocalResouce> Find(Expression<Func<LocalResouce, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<LocalResouce> Find(Expression<Func<LocalResouce, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<LocalResouce> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<LocalResouce> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<LocalResouce> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<LocalResouce> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<LocalResouce> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<LocalResouce> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "LocaleID";
        }

        public object KeyValue()
        {
            return this.LocaleID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<Guid>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.ResourceType.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(LocalResouce)){
                LocalResouce compare=(LocalResouce)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public string DescriptorValue()
        {
                            return this.ResourceType.ToString();
                    }

        public string DescriptorColumn() {
            return "ResourceType";
        }
        public static string GetKeyColumn()
        {
            return "LocaleID";
        }        
        public static string GetDescriptorColumn()
        {
            return "ResourceType";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Locale> Locales
        {
            get
            {
                
                  var repo=Vit.Web.Models.Locale.GetRepo();
                  return from items in repo.GetAll()
                       where items.LocaleID == _LocaleID
                       select items;
            }
        }

        #endregion
        

        Guid _LocaleID;
        public Guid LocaleID
        {
            get { return _LocaleID; }
            set
            {
                if(_LocaleID!=value){
                    _LocaleID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LocaleID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ResourceType;
        public string ResourceType
        {
            get { return _ResourceType; }
            set
            {
                if(_ResourceType!=value){
                    _ResourceType=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ResourceType");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ResourceUri;
        public string ResourceUri
        {
            get { return _ResourceUri; }
            set
            {
                if(_ResourceUri!=value){
                    _ResourceUri=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ResourceUri");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _LocalString;
        public string LocalString
        {
            get { return _LocalString; }
            set
            {
                if(_LocalString!=value){
                    _LocalString=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LocalString");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<LocalResouce, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Locale table in the Vit Database.
    /// </summary>
    public partial class Locale: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Locale> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Locale>(new Vit.Web.Models.VitDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Locale> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(Locale item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Locale item=new Locale();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Locale> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Vit.Web.Models.VitDB _db;
        public Locale(string connectionString, string providerName) {

            _db=new Vit.Web.Models.VitDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Locale.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Locale>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Locale(){
             _db=new Vit.Web.Models.VitDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Locale(Expression<Func<Locale, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Locale> GetRepo(string connectionString, string providerName){
            Vit.Web.Models.VitDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Vit.Web.Models.VitDB();
            }else{
                db=new Vit.Web.Models.VitDB(connectionString, providerName);
            }
            IRepository<Locale> _repo;
            
            if(db.TestMode){
                Locale.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Locale>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Locale> GetRepo(){
            return GetRepo("","");
        }
        
        public static Locale SingleOrDefault(Expression<Func<Locale, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Locale single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Locale SingleOrDefault(Expression<Func<Locale, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Locale single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Locale, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Locale, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Locale> Find(Expression<Func<Locale, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Locale> Find(Expression<Func<Locale, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Locale> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Locale> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Locale> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Locale> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Locale> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Locale> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "LocaleID";
        }

        public object KeyValue()
        {
            return this.LocaleID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<Guid>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Name.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Locale)){
                Locale compare=(Locale)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public string DescriptorValue()
        {
                            return this.Name.ToString();
                    }

        public string DescriptorColumn() {
            return "Name";
        }
        public static string GetKeyColumn()
        {
            return "LocaleID";
        }        
        public static string GetDescriptorColumn()
        {
            return "Name";
        }
        
        #region ' Foreign Keys '
        public IQueryable<LocalResouce> LocalResouces
        {
            get
            {
                
                  var repo=Vit.Web.Models.LocalResouce.GetRepo();
                  return from items in repo.GetAll()
                       where items.LocaleID == _LocaleID
                       select items;
            }
        }

        public IQueryable<User> Users
        {
            get
            {
                
                  var repo=Vit.Web.Models.User.GetRepo();
                  return from items in repo.GetAll()
                       where items.LocaleID == _LocaleID
                       select items;
            }
        }

        #endregion
        

        Guid _LocaleID;
        public Guid LocaleID
        {
            get { return _LocaleID; }
            set
            {
                if(_LocaleID!=value){
                    _LocaleID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LocaleID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Name;
        public string Name
        {
            get { return _Name; }
            set
            {
                if(_Name!=value){
                    _Name=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Name");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Country;
        public string Country
        {
            get { return _Country; }
            set
            {
                if(_Country!=value){
                    _Country=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Country");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Culture;
        public string Culture
        {
            get { return _Culture; }
            set
            {
                if(_Culture!=value){
                    _Culture=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Culture");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Language;
        public string Language
        {
            get { return _Language; }
            set
            {
                if(_Language!=value){
                    _Language=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Language");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        decimal _TimeZoneOffset;
        public decimal TimeZoneOffset
        {
            get { return _TimeZoneOffset; }
            set
            {
                if(_TimeZoneOffset!=value){
                    _TimeZoneOffset=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="TimeZoneOffset");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Currency;
        public string Currency
        {
            get { return _Currency; }
            set
            {
                if(_Currency!=value){
                    _Currency=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Currency");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _LongDateFormat;
        public string LongDateFormat
        {
            get { return _LongDateFormat; }
            set
            {
                if(_LongDateFormat!=value){
                    _LongDateFormat=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LongDateFormat");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ShortDateFormat;
        public string ShortDateFormat
        {
            get { return _ShortDateFormat; }
            set
            {
                if(_ShortDateFormat!=value){
                    _ShortDateFormat=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ShortDateFormat");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _LongTimeFormat;
        public string LongTimeFormat
        {
            get { return _LongTimeFormat; }
            set
            {
                if(_LongTimeFormat!=value){
                    _LongTimeFormat=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LongTimeFormat");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ShortTimeFormat;
        public string ShortTimeFormat
        {
            get { return _ShortTimeFormat; }
            set
            {
                if(_ShortTimeFormat!=value){
                    _ShortTimeFormat=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ShortTimeFormat");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _FullNameFormat;
        public string FullNameFormat
        {
            get { return _FullNameFormat; }
            set
            {
                if(_FullNameFormat!=value){
                    _FullNameFormat=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="FullNameFormat");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _DigitFormat;
        public string DigitFormat
        {
            get { return _DigitFormat; }
            set
            {
                if(_DigitFormat!=value){
                    _DigitFormat=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="DigitFormat");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _PostCodeFormat;
        public string PostCodeFormat
        {
            get { return _PostCodeFormat; }
            set
            {
                if(_PostCodeFormat!=value){
                    _PostCodeFormat=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PostCodeFormat");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _PhoneFormat;
        public string PhoneFormat
        {
            get { return _PhoneFormat; }
            set
            {
                if(_PhoneFormat!=value){
                    _PhoneFormat=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PhoneFormat");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Locale, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
}
